const path = require('path');
const express = require('express');
const logger = require('morgan');
const nunjucks = require('nunjucks');
const session = require('express-session');

// 导入路由
const pandaRouter = require('./routes/panda.js');
const deptRouter = require('./routes/dept.js');
const empRouter = require('./routes/emp.js');
const studentRouter = require('./routes/student.js');

const port = 3000;
const app = express();

// 设置 模板文件 根路径
const templateRoot = 'views';
// 指定模板文件名称后缀
const suffix = 'njk' ; 
// 设置模板引擎
app.set('views', path.resolve(__dirname, templateRoot ));
app.set('view engine' , suffix );

// 配置 nunjucks 引擎
nunjucks.configure( templateRoot , {
    autoescape: true ,
    express: app
});

app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

const sessionMiddleware = session({ 
    name: 'SESSIONID',
    secret:'ecut', 
    resave: false, 
    saveUninitialized: true, 
    cookie:{
        path: '/' ,
        secure: false,
        httpOnly: true,
    } 
});

app.use( sessionMiddleware );

// 启用路由
app.use(pandaRouter);
app.use(deptRouter);
app.use(empRouter);
app.use(studentRouter);

app.listen(port, ()=> console.log(`正在监听${port}端口`) );